package com.example.petstoreservice.service;

import com.example.petstoreservice.entity.Review;
import com.example.petstoreservice.mapper.ReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReviewService {
    @Autowired
    private ReviewMapper reviewMapper;

    public List<Review> getAllReviews() {
        return reviewMapper.getAllReviews();
    }

    public List<Review> getReviewsByProductId(int productId) {
        return reviewMapper.getReviewsByProductId(productId);
    }

    public boolean insertReview(Review review) {
        Date now = new Date();
        review.setCreateTime(now);
        review.setUpdateTime(now);

        int affectedRows = reviewMapper.insertReview(review);
        return affectedRows > 0;
    }


    public boolean updateReview(Review review) {
        int affectedRows = reviewMapper.updateReview(review);
        return affectedRows > 0;
    }

    public boolean deleteReview(int reviewId) {
        int affectedRows = reviewMapper.deleteReview(reviewId);
        return affectedRows > 0;
    }
    public List<Review> getReviewsByUsername(String username) {
        return reviewMapper.getReviewsByUsername(username);
    }
}
